//
//  DetailGalVC.m
//  HomeFurnishing
//
//  Created by dllo on 16/4/5.
//  Copyright © 2016年 YMing. All rights reserved.
//

#import "DetailGalVC.h"
#import "GalBaseModel.h"
@interface DetailGalVC ()<UIScrollViewDelegate>
@property(nonatomic, retain)UIScrollView *scrView;
@property(nonatomic, retain)UIImageView *imgView;
@property(nonatomic, retain)UIButton *rightButton;
@end

@implementation DetailGalVC

-(void)dealloc
{
    [_scrView release];
    [_indexP release];
    [_dataArr release];
    [_imgView release];
    [_rightButton release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    [self prefersStatusBarHidden];
    [self createView];
    [self createScrollView];
    [self createFootView];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   
    self.rightButton.selected = NO;
}


/**
 *  状态栏隐藏/显示
 *
 *  @return 隐藏
 */
-(BOOL)prefersStatusBarHidden
{
    return YES;
}
/**
 *  添加导航栏视图
 */
-(void)createView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:view];
    [view release];
    UIButton *leftBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    leftBtn.frame = CGRectMake(20, 34, 30, 30);
    [leftBtn setTintColor:[UIColor whiteColor]];
    [leftBtn setImage:[UIImage imageNamed:@"iconfont-fanhui@2x"] forState:(UIControlStateNormal)];
    [leftBtn addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:leftBtn];
    self.rightButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.rightButton.frame = CGRectMake(SCREEN_WIDTH - 50, 34, 30, 30);
    [self.rightButton setTintColor:[UIColor whiteColor]];
    [self.rightButton setImage:[UIImage imageNamed:@"iconfont-fenxiang@2x"] forState:(UIControlStateNormal)];
    [self.rightButton addTarget:self action:@selector(collectBtn:) forControlEvents:(UIControlEventTouchUpInside)];
    CGFloat i = (_scrView.contentOffset.x+SCREEN_WIDTH)/SCREEN_WIDTH;
    NSInteger j = i;
    GalBaseModel *model = _dataArr[j];
    self.rightButton.selected = [[DataBaseManager shareInstance] selectCaseBymId:model.mId.description];
    [self.view addSubview:self.rightButton];
}

-(void)createFootView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT -49, SCREEN_WIDTH, 49)];
    view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:view];
    [view release];
    UIButton *leftBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    leftBtn.frame = CGRectMake(40, 9, 30, 30);
    [leftBtn setTintColor:[UIColor whiteColor]];
    [leftBtn setImage:[UIImage imageNamed:@"iconfont-dianzan@2x"] forState:(UIControlStateNormal)];
//    [leftBtn addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    [view addSubview:leftBtn];
    UIButton *middleBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    middleBtn.frame = CGRectMake(120, 9, 30, 30);
    [middleBtn setTintColor:[UIColor whiteColor]];
    [middleBtn setImage:[UIImage imageNamed:@"iconfont-xiaoxi@2x"] forState:(UIControlStateNormal)];
//    [rightBtn addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    [view addSubview:middleBtn];
    UIButton *rightBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    rightBtn.frame = CGRectMake(SCREEN_WIDTH /2, 9, SCREEN_WIDTH /2, 30);
    [rightBtn setTintColor:[UIColor whiteColor]];
    [rightBtn setTitle:@"免费设计" forState:(UIControlStateNormal)];
//    [rightBtn addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    
    [view addSubview:rightBtn];
}
-(void)createScrollView
{
    self.scrView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 -49)];
    _scrView.contentSize = CGSizeMake(SCREEN_WIDTH *_dataArr.count, SCREEN_HEIGHT - 64 -49);
    _scrView.alwaysBounceHorizontal = YES;
    _scrView.backgroundColor = [UIColor blackColor];
    _scrView.pagingEnabled = YES;
    _scrView.bounces = YES;
    _scrView.delegate = self;
    _scrView.contentOffset = CGPointMake(SCREEN_WIDTH *_indexP.item, 0);
    _scrView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_scrView];
    [_scrView release];
    [self addImageView];
}
-(void)addImageView
{
    for (NSInteger i = 0; i<_dataArr.count; i++) {
        GalBaseModel *model = _dataArr[i];
        NSString *str = [NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w%@_h%@_m0.jpg",model.mId,model.width,model.height];
        self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH *i, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 -49)];
        self.imgView.contentMode = UIViewContentModeScaleAspectFit;
        self.imgView.clipsToBounds = YES;
        [self.imgView sd_setImageWithURL:[NSURL URLWithString:str]];
        [_scrView addSubview:_imgView];
        [_imgView release];
    }
}
-(void)collectBtn:(UIButton *)btn
{
    CGFloat i = (_scrView.contentOffset.x+SCREEN_WIDTH)/SCREEN_WIDTH;
    NSInteger j = i;
    GalBaseModel *model = _dataArr[j];
    if (btn.selected) {
        [[DataBaseManager shareInstance] deleteBymId:model.mId];
    }else{
        
        [[DataBaseManager shareInstance] insertGalBaseModel:model];
    }
    btn.selected = !btn.selected;
    // 关键帧动画 改变view缩放属性
    CAKeyframeAnimation *likeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    // 设置关键帧的值,values是数组类型
    likeAnimation.values = @[@(0.1),@(1.0),@(1.5)];
    likeAnimation.duration = 0.2;
    [btn.layer addAnimation:likeAnimation forKey:@"key"];
}
-(void)back
{
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
